package poi_readOfficeByJava;

import java.io.FileInputStream;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * @author wanghao7@asiainfo-linkage.com
 * @version create date：Sep 13, 2011 5:48:31 PM
 * @description
 */
public class ReadTest {
	public static void main(String[] args) throws Exception {
		String fileName = "d:/test.xlsx";
		Workbook wb = new XSSFWorkbook(new FileInputStream(fileName));
		for (int k = 0; k < wb.getNumberOfSheets(); k++) {
			Sheet sheet = wb.getSheetAt(k);
			int rows = sheet.getPhysicalNumberOfRows();
			System.out.println("Sheet " + k + " \"" + wb.getSheetName(k)
					+ "\" has " + rows + " row(s).");

			for (int r = 0; r < rows; r++) {
				Row row = sheet.getRow(r);
				if (row == null) {
					continue;
				}

				// int cells = row.getPhysicalNumberOfCells();
				int colCount = row.getLastCellNum();

				System.out.println("\nROW " + row.getRowNum() + " colCount is "
						+ colCount + " .");

				for (int c = 0; c < colCount; c++) {
					Cell cell = row.getCell(c);
					String value = null;
					if (cell == null) {
						System.out.println("null");
						continue;
					}

					switch (cell.getCellType()) {

					case Cell.CELL_TYPE_FORMULA:
						value = "FORMULA value=" + cell.getCellFormula();
						break;

					case Cell.CELL_TYPE_NUMERIC:
						value = "NUMERIC value=" + cell.getNumericCellValue();
						break;

					case Cell.CELL_TYPE_STRING:
						value = "STRING value=" + cell.getStringCellValue();
						break;

					default:
					}
					System.out.println("CELL col=" + cell.getColumnIndex()
							+ " VALUE=" + value);
				}
			}
		}
	}
}
